//
//  JSMDreamCityPrefectureCollectionViewCell.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/1.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMDreamCityPrefectureCollectionViewCell.h"
#import "JSMDreamCityTableViewCell.h"

@implementation JSMDreamCityPrefectureCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionViewCell];
    }
    return self;
}
#pragma mark - Method createCollectionViewCell
- (void)createCollectionViewCell {
    self.discoverArr = [JSMCollectionOfMessage getModelToPlistType:CollectionOfMessageTypeDreamCityPrefectureModel];
    // 下拉刷新
    [self addHeaderRefresh];
}
#pragma mark - Method discoverTableView 第一个tableview
- (UITableView *)discoverTableView {
    if (!_discoverTableView) {
        _discoverTableView = [[BaseTableView alloc] init];
        _discoverTableView.dataSource = self;
        _discoverTableView.delegate = self;
        _discoverTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
        [self addSubview:_discoverTableView];
        
        [_discoverTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return _discoverTableView;
}
#pragma mark - talbView的公用方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.discoverArr.count;
}
#pragma mark - Method tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200 * ALLEDITIONWIDTH;
}
#pragma mark - Method tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"discoverTableViewCellReuse";
    JSMDreamCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[JSMDreamCityTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    JSMDreamCityPrefectureModel *temp = self.discoverArr[indexPath.row];
    cell.prefectureModel = temp;
    return cell;
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
    [AFNetworkingTool getUrl:@"http://tubu.ibuzhai.com/rest/v1/trail/types?&api_version=1&app_version=4.3.3&device_type=2" hudSuperView:self result:JSON success:^(id result) {
        NSDictionary *dic = result;
        self.discoverArr = [JSMDreamCityPrefectureModel baseModelByArr:dic[@"types"]];
        [JSMCollectionOfMessage saveModelToPlist:self.discoverArr type:CollectionOfMessageTypeDreamCityPrefectureModel];
        [self.discoverTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}
#pragma mark - addHeaderRefresh 下拉刷新
- (void)addHeaderRefresh {
    self.discoverTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataFromNetWorking];
        [self.discoverTableView.header endRefreshing];
    }];
    [self.discoverTableView.header beginRefreshing];
}
#pragma mark - Method 专区点击跳转到专区详细画面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate DreamCityPrefecturePushToJSMDreamCityPrefectureDetailsViewController:self.discoverArr[indexPath.row]];
}


@end
